package com.powernode.teatraceability.service.plateprocessmgt.impl;

import com.powernode.teatraceability.dao.basicinfomgt.EnterpriseMapper;
import com.powernode.teatraceability.dao.basicinfomgt.OriginMapper;
import com.powernode.teatraceability.dao.plateprocessmgt.ProductionEnvironmentMapper;
import com.powernode.teatraceability.pojo.plateprocessmgt.ProductionEnvironment;
import com.powernode.teatraceability.service.plateprocessmgt.ProductionEnvironmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
@Transactional
public class ProductionEnvironmentServiceImpl implements ProductionEnvironmentService {

    @Resource
    private ProductionEnvironmentMapper productionEnvironmentMapper;
    @Resource
    private EnterpriseMapper enterpriseMapper;

    @Resource
    private OriginMapper originMapper;

    @Override
    public List<ProductionEnvironment> selectAll(String qydm) {
        List<ProductionEnvironment> productionEnvironments = productionEnvironmentMapper.selectAll(qydm);
        final String qymc = enterpriseMapper.selectQYMCByQydm(qydm);
        productionEnvironments.forEach(productionEnvironment -> productionEnvironment.setQymc(qymc));
        return productionEnvironments;
    }

    @Override
    public List<String> getAllCdName(String qydm) {
        return productionEnvironmentMapper.selectCdName(qydm);
    }

    @Override
    public Integer update(ProductionEnvironment productEVi) {
        return productionEnvironmentMapper.update(productEVi);
    }

    @Override
    public Integer insert(ProductionEnvironment productEVi) {
        originMapper.updateStatus(productEVi.getChandi(), true);
        return productionEnvironmentMapper.insert(productEVi);
    }

    @Override
    public List<ProductionEnvironment> selectLikeByChandinName(String chandi, String qydm) {
        final List<ProductionEnvironment> productionEnvironments = productionEnvironmentMapper.selectLikeByChandi(chandi, qydm);
        productionEnvironments.forEach(productionEnvironment -> productionEnvironment.setQymc(enterpriseMapper.selectQYMCByQydm(qydm)));
        return productionEnvironments;
    }

    @Override
    public Integer delete(String bh) {
        final String chandi = productionEnvironmentMapper.getChandi(bh);
        originMapper.updateStatus(chandi,false);
        return productionEnvironmentMapper.delete(bh);
    }
}
